import {BaseService} from "@/service";
import {ChangePassword, LoginUser, RegisterUser} from "@/entity/auth/AuthEntity";
import {Response} from "@/entity/net";
import {JwtToken} from "@/entity/net/Token";
import {NetSetting} from "@/setting";

/**
 * 用户认证相关接口
 */

/**
 * 登陆服务
 * @param loginForm
 */
export const loginService = (loginForm: LoginUser): Response<JwtToken> => {
    return BaseService.get(
        "/authen/login",
        {
            params: {
                email: loginForm.email,
                password: loginForm.password
            }
        }
    );
};

/**
 * 注册服务
 * @param registerForm
 */
export const registerService = (registerForm: RegisterUser): Response<boolean> => {
    return BaseService.post("/authen/register", registerForm);
};

/**
 * 忘记密码服务
 */
export const changePswdService = (cpaswdForm: ChangePassword): Response<boolean> => {
    return BaseService.post("/authen/forgetPassword", {
        email: cpaswdForm.email,
        password: cpaswdForm.rePassword,
        ecode: cpaswdForm.ecode
    });
};

/**
 * 刷新Token服务
 */
export const refreshTokenService = (): Response<JwtToken> => {
    return BaseService.get("/authen/refreshToken", {
        params: {
            refresh: localStorage.getItem(NetSetting.refreshKey),
            access: localStorage.getItem(NetSetting.accessKey)
        }
    });
};

/**
 * 注销服务
 */
export const logoutService = (): Response<boolean> => {
    return BaseService.post("/auth/logout");
};

/**
 * 邮件验证码服务
 * @param mail
 */
export const authMailService = (mail: string): Response<boolean> => {
    return BaseService.get("/mail/authMail", {params: {email: mail}});
};

/**
 * 私有接口连通测试，用于测试前后端认证是否正常工作
 */
export const pongService = (): Response<any> => {
    return BaseService.get("/pong");
};